/**
 * @copyright Copyright (c) 2024 COOREAL. All rights reserved.
 * @description DeviceBuilder.ts
 * @author xuezejun @date 2025/07/02
 */

import * as BABYLON from "@babylonjs/core";
import * as GUI from "@babylonjs/gui";

export class DeviceBuilder extends GUI.Rectangle {
    private image?: GUI.Image;
    private textBlock?: GUI.TextBlock;
    constructor(name?: string, img?: string, text?: string, font?: string, size?: number, color?: string, hAline?: number, vAline?: number) {
        super(name);
        if(img) {
            this.image = new GUI.Image(name, img)
            this.addControl(this.image);
        }

        
        if(text) {
            this.textBlock = new GUI.TextBlock(name, text);
            this.addControl(this.textBlock);
            if(font) {
                this.textBlock.fontFamily = font;
            }
            if(size) {
                this.textBlock.fontSize = size;
            }
            if(color) {
                this.textBlock.color = color;
            }
            if(hAline) {
                this.textBlock.horizontalAlignment = hAline;
            }
            if(vAline) {
                this.textBlock.verticalAlignment = vAline;
            }
        }
    }

    override _draw(context: BABYLON.ICanvasRenderingContext, invalidatedRectangle?: GUI.Measure): void {
        super._draw(context, invalidatedRectangle);

        context.beginPath();
        context.lineTo(0,0);
        context.lineTo(0, this.heightInPixels);
        context.lineTo(this.widthInPixels, this.heightInPixels);
        context.lineTo(this.widthInPixels, 0);
        context.closePath();
        context.stroke();
    }

    public get text(): string {
        return this.textBlock?.text ?? '';
    }

    public set text(value: string) {
        if(this.textBlock == null) {
            this.textBlock = new GUI.TextBlock(undefined, value);
        } else {
            this.textBlock.text = value;
        }
    }
}